/**
 * 用户管理页面加载完成
 */
+(function(){
	// register the treeselect component
    //Vue.component('treeselect', VueTreeselect.Treeselect);
	var userVue=new Vue({
		el:"#userContainer",
		data:function(){
			var checkUserName=function(rule, value, callback){ // 保存时验证用户名是否可以
	        	var url=ctx+"/admin/user/checkUserName";
	        	$.ajax({url:url,type:"POST",dataType:"json",data:{userName:value},success:function(data){
	        		if(data.success==true)
	        		{
	        			callback();
	        		}
	        		else
	        		{
	        			callback(new Error('该用户名已被使用'));
	        		}
	        	}});
	        };
			return {
				loading:false,
				loadingTxt:$.admin.loadingTxt,
				tableHeight:pageContentHeight-100,
				userQryForm:{
					userName:""
				},
				tableUsers:[],
				userCurrentPage:1,
				defaultPageSize:defaultPageSize,
				userPageSize:defaultPageSize,
				userTotalCount:0,
				treeOrgOptions: [],
				editDiag:{//用户弹出框
					show:false,
					title:"新增用户",
					type:1,
					userNameProp:"userName",
					userNameDisable:true,
					formLabelWidth:"100px",
					closeBuName:"取消",
					formDisable:true,
					formUser:{
						id:"",
						userName:"",
						aliasName:"",
						phone:"",
						sex:"1",
						usable:"1",
						qq:"",
						email:"",
						birthDay:'',
						remark:"",
						orgCode:""
					},
					defultUser:{
						sex:"1",
						usable:"1",
						birthDay:''
					},
					rules:{
						userName:[
			        		{ required: true, message: '用户名不能为空', trigger: 'blur' },
			        		{ validator:checkUserName, trigger: 'blur' }
			        	],
			        	orgCode:[{ required: true, message: '所属区域不能为空', trigger: 'blur' }],
			        	sex:[{ required: true, message: '性别不能为空', trigger: 'blur' }],
			        	phone:[
			        		   {pattern : /^1[34578]\d{9}$/, message: '手机号码格式错误',trigger: 'change' }
			        		],
		        		qq:[
		        		   {pattern: /[1-9][0-9]{4,14}$/, message: 'qq格式错误', trigger: 'change' }
		        		],
		        		email:[
			        		   { type: 'email', message: '请输入正确的邮箱地址', trigger: 'change' },
			        		]
					}
				},
				userRoleDiag: //用户关联角色
				{
					title:"关联角色",
					show:false,
					selectRoles:[],
					props:{key:"id",label:"roleName"},
					roles:[],
					userId:""
				}
			};
		},
		methods:{
		   treeOrg:function(node) {
		      return {
		        id: node.sn,
		        label: node.name,
		        children: node.children,
		      }
		    },
		    qryOrgs:function()
		    {
		    	var $this=this;
		    	var url=ctx+"/fnyc/area/sf/all";
		    	$.ajax({url:url,type:"POST",dataType:"json",data:{},success:function(data){
		    		var data=data.data;
					if(data)
					{
						var tree=$.util.listToTree(data,"sn","parentSn");
						$this.treeOrgOptions=tree;
					}
				}});
		    },
			qryUser:function(page)
			{
				if(1!=page)
				{
					this.userCurrentPage=1;
					this.userTotalCount=0;
				}
				this.loading=true;
				var $this=this;
				var url=ctx+"/admin/user/qry";
				var params={userName:this.userQryForm.userName,pageIndex:this.userCurrentPage-1,pageSize:this.userPageSize};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					if(data && data.success==true)
					{
						var data=data.data;
						$this.userTotalCount=data.totalElements;
						$this.tableUsers=data.content;
					}
					$this.loading=false;
				}});
			},
			openAddUserPage:function(type,event) //打开新增/修改用户窗口
			{
				this.editDiag.show=true;
				this.editDiag.type=type;
				this.editDiag.closeBuName="取消";
				if(type==1)  //新增
				{
					this.editDiag.formUser=this.editDiag.defultUser;
					this.editDiag.formUser.id="";
					this.editDiag.title="新增用户";
					this.editDiag.userNameProp="userName";
					this.editDiag.userNameDisable=false;
				}
				else if(type==2) //修改
				{
					this.editDiag.userNameProp="";
					this.editDiag.userNameDisable=true;
					this.editDiag.title="修改用户";
					this.bindUser($(event.currentTarget).attr("userId"));
				}
				else if(type==3) //详情
				{
					this.editDiag.title="用户详情";
					this.editDiag.closeBuName="关闭";
					this.bindUser($(event.currentTarget).attr("userId"));
				}
			},
			bindUser:function(userId) //绑定弹出框用户信息
			{
				var url=ctx+"/admin/user/one";
				var $this=this;
				var params={userId:userId}
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					var data=data.data;
					if(data)
					{
						data.sex=data.sex+"";
						data.usable=data.usable+"";
						if(!data.birthDay) data.birthDay="";
						$this.editDiag.formUser=data;
					}
				}});
			},
			saveUser:function(event)  //保存
			{
				var $this=this;
				//this.editDiag.formUser;
				this.$refs["userDialogForm"].validate(function(valid){
					if(valid==true)
					{
						var msg="保存用户成功，默认密码为123456";
						if($this.editDiag.type==1)
						{
							msg="保存用户成功，默认密码为123456";
						}
						else
						{
							msg="修改用户成功";
						}
						var url=ctx+"/admin/user/save";
						var params=$this.editDiag.formUser;
						if(params.birthDay && params.birthDay!="")
						{
						   var date=new Date(params.birthDay);
						   params.birthDay=date.getFullYear()+"-"+(date.getMonth() + 1)+"-"+date.getDate();
						}
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
							if(data.success==true)
							{
								$this.editDiag.show=false;
								$this.$message({
							          message: msg,
							          type: 'info',
							          showClose: true
							      });
								$this.qryUser();
							}
							else
							{
								var errs=data.data;
								for(var i=0;i<errs.length;i++)
								{
								   var err=errs[i];
								   msg="【"+err.field+"】"+err.msg;
								   $this.$message({
								          message: msg,
								          type: 'info',
								          showClose: true
								      });
								}
							}
						}});
					}
				});
			},
			onCloseUserDialog:function(event) //重置表新增/修改用户表单
			{
				this.$refs["userDialogForm"].resetFields();
			},
			delUser:function(event)//删除用户
			{
				var $this=this;
				var $t=$(event.currentTarget);
				this.$confirm('确定要删除?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(function(){
		        	var url=ctx+"/admin/user/del";
					if($t.attr("userName")=="admin")
					{
						$this.$message({
					          message: 'admin用户不能删除！',
					          type: 'warning'
					      });
					}
					else
					{
						var params={id:$t.attr("userId")}
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
							var data=data.data;
							$this.qryUser();
							$this.$message({
					            type: 'info',
					            message: '删除成功！'
					         }); 
						}});
					}
		        }).catch(function(){
		          $this.$message({
		            type: 'info',
		            message: '已取消删除'
		          });          
		        });
			},
			updatePwd:function(event) //重置密码
			{
				var userId=$(event.currentTarget).attr("userId");
				var url=ctx+"/admin/user/reset/pwd";
				$this=this;
				$.ajax({url:url,type:"POST",dataType:"json",data:{userId:userId},success:function(data){
					$this.$message({
			            type: 'info',
			            message: '重置密码成功！'
			         }); 
				}});
			},
			handleSizeChange:function(val) {  //分页条改变显示条数
				this.userPageSize=val;
				this.qryUser();
			},
	        handleCurrentChange:function(val) { //分页条分页发生改变
	        	this.userCurrentPage=val;
	        	this.qryUser(1);
	        },
	        sexName:function(sex)
			{
	        	var sexName="-"
	        	if(sex=="1")
        		{
	        	   sexName="男";
        		}
	        	else if(sex=="2")
        		{
	        		sexName="女";
        		}
	        	return sexName;
			},
			
			//用户角色
	        openUserRoleWin:function(event){ //打开用户关联角色窗口
	        	this.userRoleDiag.show=true;
	        	this.userRoleDiag.userId=$(event.currentTarget).attr("userId");
	        	this.qryUserRole();
	        },
	        onCloseUserRoleDiag:function(e) //关闭用户关联角色弹窗
	        {
	        	this.userRoleDiag.roles=[];
	        	this.userRoleDiag.selectRoles=[];
	        },
	        saveUserRole:function(e) //保存用户关联角色
	        {
	        	var url=ctx+"/admin/user/role/save";
				var $this=this;
				var params={userId:this.userRoleDiag.userId,roleIds:this.userRoleDiag.selectRoles.toString()};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					if(data.success==true)
					{
						$this.$message({
				            type: 'success',
				            message: '操作成功'
				         });
						$this.userRoleDiag.show=false;
						$this.qryUser();
					}
					else
					{
						$this.$message({
				            type: 'warning',
				            message: data.msg
				         });
					}
				}});
	        },
	        qryUserRole:function() //查询用户关联角色
	        {
        		var url=ctx+"/admin/user/role/all";
				var $this=this;
				var params={userId:this.userRoleDiag.userId};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					var data=data.data;
					if(data)
					{
						$this.userRoleDiag.roles=data.roles;
						$this.userRoleDiag.selectRoles=data.selectRoles;
					}
				}});
	        }
		},
		computed:{
			
		}
	});
	//userVue.qryOrgs();
	userVue.qryUser();
})();
